package com.hanter.android.radwidget.statusbar;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.Window;

import androidx.annotation.NonNull;

import com.etoonet.android.radwidget.R;

public class StatusBarHelper {

    private static final String XIAOMI = "Xiaomi";
    private static final String OPPO = "OPPO";
    private static final String MEIZU = "MEIZU";

    /**
     * 系统状态栏高度
     */
    public static int statusBarHeight;
    /**
     * 自定义占位状态栏高度，在不支持状态栏的4.4一下版本为 0
     */
    public static int compatStatusBarHeight;
    private static boolean support;
    private static StatusBarModeAdapter sStatusBarModeAdapter;

    static {
        switch (Build.MANUFACTURER) {
            case XIAOMI:
                sStatusBarModeAdapter = new MIUIAdapter();
                break;

            case MEIZU:
                sStatusBarModeAdapter = new FlymeAdapter();
                break;

            case OPPO:
                sStatusBarModeAdapter = new ColorOsAdapter();
                break;

            default:
                sStatusBarModeAdapter = new DefaultAdapter();
        }
    }

    public static boolean isSupportLightMode() {
        return sStatusBarModeAdapter.isSupportLightMode() | support;
    }

    public static boolean setStatusBarMode(@NonNull Window window, boolean lightMode) {
        return support = sStatusBarModeAdapter.setStatusBarMode(window, lightMode);
    }

    public static boolean setStatusBarMode(@NonNull Activity activity, boolean lightMode) {
        return support = sStatusBarModeAdapter.setStatusBarMode(activity.getWindow(), lightMode);
    }

    public static int getStatusBarHeight(Context context) {
        if (statusBarHeight == 0) {
            int result;
            try {
                int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
                        "android");
                result = context.getResources().getDimensionPixelSize(resourceId);
                if (result == 0) {
                    result = context.getResources().getDimensionPixelSize(R.dimen.rad_status_bar_height);
                }
            } catch (Exception e) {
                result = context.getResources().getDimensionPixelSize(R.dimen.rad_status_bar_height);
            }
            statusBarHeight = result;
            compatStatusBarHeight = getCompatStatusBarHeight();
        }
        return statusBarHeight;
    }

    public static int getCompatStatusBarHeight(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return getStatusBarHeight(context);
        } else {
            return 0;
        }
    }

    public static int getCompatStatusBarHeight() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return statusBarHeight;
        } else {
            return 0;
        }
    }

}
